package com.gdk.ksoft.ui.sales.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.common.SystemData;
import com.gdk.ksoft.data.entry.tables.ShoppingCartGoodsTable;
import com.gdk.ksoft.assembly.utils.extensions.FormatUtils;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;

/**
 * Created by Mrper on 2014/12/26.
 * 买赠本品商品数据适配器
 */
public class GiveSelfAdapter extends BaseAdapter {

    private Context context;
    private List<ShoppingCartGoodsTable> list;

    public GiveSelfAdapter(Context context,List<ShoppingCartGoodsTable> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ShoppingCartGoodsTable item = list.get(position);
        final ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_pgiveself_listgoods,null);
            holder.imgIcon = inflate(convertView, R.id.item_pgiveself_listgoods_imgIcon);
            holder.txtGoodsName = inflate(convertView,R.id.item_pgiveself_listgoods_txtGoodsName);
            holder.txtGoodsFormat = inflate(convertView,R.id.item_pgiveself_listgoods_txtGoodsFormat);
            holder.txtGoodsCode = inflate(convertView,R.id.item_pgiveself_listgoods_txtGoodsCode);
            holder.txtGoodsExpression = inflate(convertView,R.id.item_pgiveself_listgoods_txtGoodsExpression);
            holder.imgCheck = inflate(convertView,R.id.item_pgiveself_listgoods_imgCheck);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        if(item.hsdwsl!=0) holder.imgCheck.setVisibility(View.VISIBLE);//核算单位数量不为0，显示check图标
        new BitmapUtils(context).configDefaultLoadingImage(R.drawable.ic_default_goods)
                .configDefaultLoadFailedImage(R.drawable.ic_default_goods)
                .display(holder.imgIcon, SystemData.BASE_URL + item.slt);
        holder.txtGoodsName.setText(item.mc);
        holder.txtGoodsFormat.setText(item.gg+item.hsdw);
        holder.txtGoodsCode.setText(item.tm);
        holder.txtGoodsExpression.setText((item.hsdwsl == 0)?"":((item.xsdwsl==0?"":item.xsdwsl+item.xsdw)+(item.xsdwss==0?"":item.xsdwss+item.hsdw)+"*"+ FormatUtils.exactNumber(item.bzxsdwj,"0.00")+"="+FormatUtils.exactNumber(item.yfje,"0.00")+"元"));
        return convertView;
    }

    public List<ShoppingCartGoodsTable> getSourceList(){
        return this.list;
    }

    public<T> T inflate(View v,int id){
        return (T)v.findViewById(id);
    }

    class ViewHolder{
        public ImageView imgIcon;
        public ImageView imgCheck;
        public TextView txtGoodsName;
        public TextView txtGoodsFormat;
        public TextView txtGoodsCode;
        public TextView txtGoodsExpression;
    }

}
